//
//  MineViewController.m
//  魔兽大数据
//
//  Created by 王右 on 2017/11/2.
//  Copyright © 2017年 王右. All rights reserved.
//

#import "MineViewController.h"
#import "BigDataProportionView.h"
#import "BigDataHeader.h"

#define BgColor [UIColor colorWithRed:230/255.0f green:253/255.0f blue:253/255.0f alpha:1]

@interface MineViewController ()<ChartViewDelegate>

@property (nonatomic, strong) PieChartView *pieChartView;
@property (nonatomic, strong) PieChartData *data;
@property (nonatomic, strong) NSMutableArray *yValues;

@end

@implementation MineViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    BigDataProportionView *view;
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"BigDataProportionView" owner:self options:nil];
    if (nibs && nibs.count) {
        view = [nibs firstObject];
    }
    view.frame = CGRectMake(0, 100, 375, 80);
    [view setDataWithAllianceNumbers:90 horderNumbers:9000];
//    [self.view addSubview:view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [RequestManager requestWithModule:@"" param:@{} success:^(NSDictionary *obj) {
//        NSLog(@"%@",obj);
//    } failure:^(NSString *error) {
//
//    }];
//
//    [RequestManager request2WithModule:@"" param:@{} success:^(NSDictionary *obj) {
//        NSLog(@"%@",obj);
//    } failure:^(NSString *error) {
//
//    }];
//
//
//    //title
//    UILabel *title_label = [[UILabel alloc] init];
////    title_label.text = @"Pie Chart";
//    title_label.font = [UIFont systemFontOfSize:45];
//    title_label.textColor = [UIColor brownColor];
//    title_label.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:title_label];
//    [title_label mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(260, 50));
//        make.centerX.equalTo(self.view.mas_centerX);
//        make.centerY.equalTo(self.view.mas_centerY).offset(-200);
//    }];
//
//    //updateData btn
//    UIButton *display_btn = [[UIButton alloc] init];
//    [display_btn setTitle:@"updateData" forState:UIControlStateNormal];
//    [display_btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//    display_btn.titleLabel.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:display_btn];
//    [display_btn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(200, 50));
//        make.centerX.equalTo(self.view.mas_centerX);
//        make.centerY.equalTo(self.view.mas_centerY).offset(240);
//    }];
//    [display_btn addTarget:self action:@selector(updateData) forControlEvents:UIControlEventTouchUpInside];
//
//    //创建饼状图
//    self.pieChartView = [[PieChartView alloc] init];
//    self.pieChartView.backgroundColor = BgColor;
//    [self.view addSubview:self.pieChartView];
//    [self.pieChartView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(300, 300));
//        make.center.mas_equalTo(self.view);
//    }];
//    //基本样式
//    [self.pieChartView setExtraOffsetsWithLeft:30 top:0 right:30 bottom:0];//饼状图距离边缘的间隙
//    self.pieChartView.usePercentValuesEnabled = YES;//是否根据所提供的数据, 将显示数据转换为百分比格式
//    self.pieChartView.dragDecelerationEnabled = YES;//拖拽饼状图后是否有惯性效果
//    self.pieChartView.drawSliceTextEnabled = NO;//是否显示区块文本
//    //空心饼状图样式
//    self.pieChartView.drawHoleEnabled = YES;//饼状图是否是空心
//    self.pieChartView.holeRadiusPercent = 0.7;//空心半径占比
//    self.pieChartView.holeColor = [UIColor clearColor];//空心颜色
//    self.pieChartView.transparentCircleRadiusPercent = 0.7;//半透明空心半径占比
//    self.pieChartView.transparentCircleColor = [UIColor colorWithRed:210/255.0 green:145/255.0 blue:165/255.0 alpha:0.3];//半透明空心的颜色
//    //饼状图中间描述
//    self.pieChartView.drawCenterTextEnabled = YES;//是否显示中间文字
//    //富文本
//    NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"饼状图\n天下第一\n宇宙无敌"];
//    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
//    paragraph.alignment = NSTextAlignmentCenter;//设置对齐方式
//
//    [centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20],
//                                NSForegroundColorAttributeName: [UIColor orangeColor]}
//                        range:NSMakeRange(0,@"饼状图\n".length)];
//    [centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:10],
//                                NSForegroundColorAttributeName: [UIColor redColor]}
//                        range:NSMakeRange(@"饼状图\n".length, @"天下第一\n".length)];
//    [centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:10],
//                                NSForegroundColorAttributeName: [UIColor blueColor]}
//                        range:NSMakeRange(@"饼状图\n".length + @"天下第一\n".length, @"宇宙无敌".length)];
//    [centerText addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, centerText.length)];
//        self.pieChartView.centerAttributedText = centerText;
//    //饼状图描述
//    self.pieChartView.descriptionText = @"";
//    self.pieChartView.descriptionFont = [UIFont systemFontOfSize:10];
//    self.pieChartView.descriptionTextColor = [UIColor grayColor];
//    //饼状图图例
////    self.pieChartView.legend.maxSizePercent = 1;//图例在饼状图中的大小占比, 这会影响图例的宽高
////    self.pieChartView.legend.formToTextSpace = 5;//文本间隔
////    self.pieChartView.legend.font = [UIFont systemFontOfSize:10];//字体大小
////    self.pieChartView.legend.textColor = [UIColor grayColor];//字体颜色
////    self.pieChartView.legend.position = ChartLegendPositionBelowChartCenter;//图例在饼状图中的位置
////    self.pieChartView.legend.form = ChartLegendFormCircle;//图示样式: 方形、线条、圆形
////    self.pieChartView.legend.formSize = 12;//图示大小
//    self.pieChartView.legend.enabled = NO;//关闭图例显示
//
//    //为饼状图提供数据
//    self.data = [self setData];
//    self.pieChartView.data = self.data;
//    //设置动画效果
//    [self.pieChartView animateWithXAxisDuration:1.0f easingOption:ChartEasingOptionEaseOutExpo];
//    self.pieChartView.delegate = self;
    
}

- (void)updateData{
    //为饼状图提供数据
    self.data = [self setData];
    self.pieChartView.data = self.data;
    //设置动画效果
    [self.pieChartView animateWithXAxisDuration:1.0f easingOption:ChartEasingOptionEaseOutExpo];
}

- (PieChartData *)setData{
    
    double mult = 100;
    int count = 5;//饼状图总共有几块组成
    
    //每个区块的数据
    _yValues = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++) {
        double randomVal = arc4random_uniform(mult + 1);
        PieChartDataEntry *entry = [[PieChartDataEntry alloc] initWithValue:randomVal label:[NSString stringWithFormat:@"第%d个",i]];
        [_yValues addObject:entry];
    }
    //dataSet
    
    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:_yValues label:@""];
    dataSet.drawValuesEnabled = NO;//是否绘制显示数据
    NSMutableArray *colors = [[NSMutableArray alloc] init];
    [colors addObjectsFromArray:ChartColorTemplates.vordiplom];
    [colors addObjectsFromArray:ChartColorTemplates.joyful];
    [colors addObjectsFromArray:ChartColorTemplates.colorful];
    [colors addObjectsFromArray:ChartColorTemplates.liberty];
    [colors addObjectsFromArray:ChartColorTemplates.pastel];
    [colors addObject:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
    dataSet.colors = colors;//区块颜色
    dataSet.sliceSpace = 0;//相邻区块之间的间距
    dataSet.selectionShift = 8;//选中区块时, 放大的半径
    dataSet.xValuePosition = PieChartValuePositionInsideSlice;//名称位置
    dataSet.yValuePosition = PieChartValuePositionOutsideSlice;//数据位置
    //数据与区块之间的用于指示的折线样式
//    dataSet.valueLinePart1OffsetPercentage = 0.85;//折线中第一段起始位置相对于区块的偏移量, 数值越大, 折线距离区块越远
//    dataSet.valueLinePart1Length = 0.5;//折线中第一段长度占比
//    dataSet.valueLinePart2Length = 0.4;//折线中第二段长度最大占比
//    dataSet.valueLineWidth = 1;//折线的粗细
    dataSet.valueLineColor = [UIColor clearColor];//折线颜色
    
    //data
    PieChartData *data = [[PieChartData alloc] initWithDataSets:@[dataSet]];
//    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
//    formatter.numberStyle = NSNumberFormatterPercentStyle;
//    formatter.maximumFractionDigits = 0;//小数位数
//    formatter.multiplier = @1.f;
//    ChartDefaultValueFormatter *charformat = [[ChartDefaultValueFormatter alloc] initWithFormatter:formatter];
//    [data setValueFormatter:charformat];//设置显示数据格式
//    [data setValueTextColor:[UIColor brownColor]];
//    [data setValueFont:[UIFont systemFontOfSize:10]];
    
    return data;
}

- (void)chartValueSelected:(ChartViewBase *)chartView entry:(ChartDataEntry *)entry highlight:(ChartHighlight *)highlight{
//    NSInteger index = [_yValues indexOfObject:entry];
}

- (void)chartValueNothingSelected:(ChartViewBase *)chartView{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
